<template>
  <el-dialog :visible.sync="visible" title="工作流导入"
             :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-row>
      <el-col style="text-align: right" :span="8">
        导入工作流文件
      </el-col>
      <el-col :span="14" :offset="1">
        <el-upload
          class="upload-demo"
          ref="upload"
          :action="importUrl"
          :multiple="falseFlag"
          :headers="headers"
          :limit="1"
          accept="application/x-zip-compressed"
          :file-list="fileList"
          :on-exceed="fileExceed"
          :on-success="uploadSucc"
          :auto-upload="false">
          <el-button slot="trigger" size="small" type="primary">选取模型文件</el-button>
          <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">导入模型</el-button>
          <div slot="tip" class="el-upload__tip">只能上传zip文件，且不超过10mb</div>
        </el-upload>
      </el-col>
    </el-row>
    <el-row style="margin-top: 15px;">
      <el-col style="text-align: right" :span="8">
        导入结果
      </el-col>
      <el-col :span="14" :offset="1"></el-col>
    </el-row>
    <el-row style="margin-top: 15px;">
      <el-col>
        <template>
          <el-table
            :data="impData"
            stripe
            style="width: 100%">
            <el-table-column
              type="index"
              width="50">
            </el-table-column>
            <el-table-column
              prop="step"
              label="步骤"
              width="160">
            </el-table-column>
            <el-table-column
              prop="flag"
              label="状态"
              width="80">
              <template slot-scope="scope">
                <el-tag
                  v-if="scope.row.flag === 'T'"
                  type="success">成功
                </el-tag>
                <el-tag
                  v-if="scope.row.flag === 'F'"
                  type="danger">失败
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column
              prop="desc"
              label="描述">
            </el-table-column>
          </el-table>
        </template>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      falseFlag: false,
      trueFlag: true,
      headers: {
        token: this.$cookies.get('token')
      },
      importUrl: `${window.SITE_CONFIG['apiURL']}/act/model/import`,
      fileList: [],
      impData: []
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs.upload.clearFiles()
        this.impData = []
      })
    },
    submitUpload () {
      this.$refs.upload.submit()
    },
    fileExceed (files, fileList) {
      this.$message.error('只能选择一个文件上传')
    },
    uploadSucc (res, file, fileList) {
      this.$refs.upload.clearFiles()
      if (res.code !== 0) {
        return this.$message.error(res.msg)
      } else {
        this.$message.success(this.$t('prompt.success'))
        this.impData = res.data
      }
    }
  }
}
</script>
